package com.xrz.sxm.aj.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.xrz.sxm.aj.activity.R;
import com.xrz.sxm.aj.cons.BuWei;
import com.xrz.sxm.aj.cons.BuweiCons;
import com.xrz.sxm.aj.entity.BuweiEntity;

import java.util.ArrayList;
import java.util.List;

public class BuweiViewGroup extends LinearLayout implements OnClickListener{

	private LayoutInflater m_Inflater;

	private List<RadioButton> m_Buttons;
	/** RadioButton的布局参数 */
	private LayoutParams params;

	private onBuweiCliclListener listener;

	public BuweiViewGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public BuweiViewGroup(Context context) {
		super(context);
		init();
	}

	private void init() {
		setPadding(3, 3, 3, 6);
		params = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		params.weight = 1;
		params.topMargin = 4;
		setOrientation(VERTICAL);
		m_Buttons = new ArrayList<RadioButton>();
		m_Inflater = LayoutInflater.from(getContext());
		setBackgroundResource(R.mipmap.status_bak);
		List<BuweiEntity> list = BuweiCons.getInstant().getBuweis();
		LinearLayout layout1 = new LinearLayout(getContext());
		layout1.setOrientation(LinearLayout.HORIZONTAL);
		for (int i = 0; i < 8; i++) {
			BuweiEntity entity = list.get(i);
			LinearLayout button = createRadioButton(entity.buweiName,
					entity.enumBuWei);
			layout1.addView(button);
		}

		LinearLayout layout2 = new LinearLayout(getContext());
		layout2.setOrientation(LinearLayout.HORIZONTAL);
		for (int i = 8; i < 16; i++) {
			if (i < 12) {
				BuweiEntity entity = list.get(i);
				LinearLayout button = createRadioButton(entity.buweiName,
						entity.enumBuWei);
				layout2.addView(button);
			} else {
				LinearLayout button = createRadioButton("   ", null);
				layout2.addView(button);
			}
		}

		addView(layout1);
		addView(layout2);
	}

	private LinearLayout createRadioButton(String text, BuWei buWei) {
		LinearLayout layout = new LinearLayout(getContext());
		layout.setGravity(Gravity.CENTER);
		RadioButton rb = (RadioButton) m_Inflater.inflate(
				R.layout.item_radiobutton, null);
		layout.setLayoutParams(params);
		if (buWei != null) {
			rb.setOnClickListener(this);
			m_Buttons.add(rb);
			rb.setTag(buWei);
		} else
			rb.setBackgroundDrawable(null);
		rb.setText(text);
		layout.addView(rb);
		return layout;
	}
	/**
	 * 设置默认的选中项
	 * @param buWei
	 */
	public void setDefaultSelect(BuWei buWei){
		for(RadioButton button:m_Buttons){
			button.setChecked(false);
			if(button.getTag().equals(buWei)){
				button.setChecked(true);
				if(this.listener != null)
					listener.onBuweiCLick((BuWei)button.getTag(), button.getText().toString());
			}
		}
	}

	@Override
	public void onClick(View arg0) {
		for(RadioButton button:m_Buttons){
			button.setChecked(false);
			if(arg0.getTag().equals(button.getTag())){
				button.setChecked(true);
				if(this.listener != null){
					listener.onBuweiCLick((BuWei)button.getTag(), button.getText().toString());
				}
			}
		}
	}

	public interface onBuweiCliclListener{
		void onBuweiCLick(BuWei buWei,String buweiName);
	}

	public void setOnBuweiClickListener(onBuweiCliclListener listener){
		this.listener = listener;
	}

}
